﻿using Microsoft.AspNetCore.Mvc;
using WCS_Entity.Dto.AutoEquipmentDto;
using WCS_Entity.Dto;
using WCS_Service.Interface;
using WCS_Service.Services;
using WCS_Entity.Dto.RcsDto;

namespace WCS.Controllers
{
    /// <summary>
    /// RCSAPI管理接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class RcsApiController : ControllerBase
    {
        private readonly IRcsApiService _rcsApiService;

        public RcsApiController(IRcsApiService rcsApiService)
        {
            _rcsApiService = rcsApiService;
        }

        #region RcsApi管理
        /// <summary>
        /// 查询指定RcsApi的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> QueryRcsApiAsync(int id)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _rcsApiService.QueryRcsApiAsync(id);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "RcsApi信息获取失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 查询所有RcsApi的信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryAllRcsApiAsync()
        {
            var result = new WebApiCallBack();
            var dtoInfoList = await _rcsApiService.QueryAllRcsApiAsync();
            if (dtoInfoList is null)
            {
                result.status = false;
                result.msg = "所有RcsApi信息获取失败";
                return result;
            }
            result.data = dtoInfoList;
            return result;
        }
        /// <summary>
        /// 查询指定RCS的所有API
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryRcsApiBaseRcsIdAsync([FromBody] int id)
        {
            var result = new WebApiCallBack();
            var dtoInfoList = await _rcsApiService.QueryRcsApiBaseRcsIdAsync(id);
            if (dtoInfoList is null)
            {
                result.status = false;
                result.msg = "RcsApi信息获取失败";
                return result;
            }
            result.data = dtoInfoList;
            return result;
        }
        /// <summary>
        /// 删除指定的RcsApi设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<WebApiCallBack> DeleteAutoEquipmentAsync(int id)
        {
            var result = new WebApiCallBack();
            result.status = await _rcsApiService.DeleteRcsApi(id);
            result.msg = result.status ? "删除成功" : "删除失败";

            return result;
        }
        /// <summary>
        /// 创建RcsApi
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> CreatRcsApiAsync([FromBody] CreateRcsApiDto dto)
        {
            var result = new WebApiCallBack();
            if (string.IsNullOrEmpty(dto.apiName) ||
                dto.rcsId == 0 ||
                string.IsNullOrEmpty(dto.route))
            {
                result.status = false;
                result.msg = "api名称、rcsId，路由为空";
                return result;
            }
            var dtoInfo = await _rcsApiService.CreateRcsApiAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "创建RcsApi信息失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 更新RcsApi信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateRcsApiAsync([FromBody] UpdateRcsApiDto dto)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _rcsApiService.UpdateRcsApiAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "更新RcsApi失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        #endregion

    }
}
